package com.rgs.se.data.stack;


import com.rgs.se.data.list.SimpleList;

/**
 * @author Ren Gaoshuai
 * @ClassName ListStack
 * @date 2022-04-07 21:21
 **/
public class ListStack<E> implements Stack<E> {

    private SimpleList<E> list;

    public ListStack(){
        list = new SimpleList<>();
    }

    @Override
    public int getSize() {
        return list.getSize();
    }

    @Override
    public boolean isEmpty() {
        return list.isEmpty();
    }

    @Override
    public void push(E e) {
        list.addFirst(e);
    }

    @Override
    public E pop() {
        return list.deleteFirst();
    }

    @Override
    public E peek() {
        return list.getFirst();
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("Stack:top ");
        builder.append(list);
        return builder.toString();
    }

    public static void main(String[] args) {
        ListStack<Integer> list = new ListStack<>();

        for (int i = 0; i < 5; i++) {
            list.push(i);
        }
        System.out.println(list);
        list.pop();
        System.out.println(list);
    }
}
